<?php  
    if (!defined('BASEPATH')) exit('No direct script access allowed');

    class Cropimage
    {

        /*
        * Copyright (c) 2008 http://www.webmotionuk.com / http://www.webmotionuk.co.uk
        * "PHP & Jquery image upload & crop"
        * Date: 2008-11-21
        * Ver 1.2
        * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
        *
        * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
        * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
        * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
        * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
        * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
        * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
        * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
        * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
        * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        *
        */          

        #########################################################################################################
        # CONSTANTS                                                                                             #
        # You can alter the options below                                                                       #
        #########################################################################################################
        var $random_key;
        
        var $max_file;                             // Maximum file size in MB
        var $max_width;                            // Max width allowed for the large image
        var $thumb_width;                          // Width of thumbnail image
        var $thumb_height;                         // Height of thumbnail image         
        
        var $original_image_name;                  // Image Name 
        var $original_image_location;              // Image Location
        var $full_image_path;                      // Image Path 
        
        var $thumb_image_prefix;                   // The prefix name to the thumb image
        var $thumb_image_name;                     // New name of the thumbnail image (append the timestamp to the filename)
        var $thumb_image_location;                 // Thumb Location
        var $full_thumb_path;                      // Thumb Path 

        public function __construct($params)
        {
            // Do something with $params
            if (!isset($this->random_key) || strlen($this->random_key)==0){
                $this->random_key = strtotime(date('Y-m-d H:i:s'));
            }
                 
            $this->thumb_image_prefix = $params['thumb_image_prefix'];    
            $this->max_file = $params['max_file'];;
            $this->max_width = $params['max_width'];
            $this->thumb_width = $params['thumb_width'];
            $this->thumb_height = $params['thumb_height'];

            $this->original_image_name = $params['original_image_name'];
            $this->original_image_location = $params['original_image_location'];
            $this->full_image_path =  $this->original_image_location. $this->original_image_name;
            
            $this->thumb_image_name = $this->thumb_image_prefix.$this->random_key.'_'.$params['thumb_image_name'];
            $this->thumb_image_location = $params['thumb_image_location'];
            $this->full_thumb_path =  $this->thumb_image_location. $this->thumb_image_name;            


        }

        //Check to see if any images with the same name already exist  
        function checkFileExisit(){
            
            if (!file_exists($this->full_image_path)){ 
                
                return FALSE; 

            }else{

                return TRUE;

            }
        }
        
        public function getFullImagePath(){
            return $this->full_image_path;
        }
        public function getFullThumbPath(){
            return $this->full_thumb_path;
        }
        public function getThumbImageName(){
            return $this->thumb_image_name;
        }
        

        ##########################################################################################################
        # IMAGE FUNCTIONS                                                                                        #
        # You do not need to alter these functions                                                               #
        ##########################################################################################################
        function createThumbnail($params){

            if(!$this->checkFileExisit())return FALSE;
            
            
                //Get the new coordinates to crop the image.
                //echo '<br />Get the new coordinates to crop the image.';
                $x1 = $params["x1"];
                $y1 = $params["y1"];
                $x2 = $params["x2"];
                $y2 = $params["y2"];
                $w = $params["w"];
                $h = $params["h"];

                //Scale the image to the thumb_width set above
                $scale = $this->thumb_width/$w;
                $cropped = $this->resizeThumbnailImage($this->full_thumb_path, $this->full_image_path,$w,$h,$x1,$y1,$scale);

                 return TRUE; 
            

        }

        function resizeImage($image,$width,$height,$scale) {
            list($imagewidth, $imageheight, $imageType) = getimagesize($image);
            $imageType = image_type_to_mime_type($imageType);
            $newImageWidth = ceil($width * $scale);
            $newImageHeight = ceil($height * $scale);
            $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
            switch($imageType) {
                case "image/gif":
                    $source=imagecreatefromgif($image); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    $source=imagecreatefromjpeg($image); 
                    break;
                case "image/png":
                case "image/x-png":
                    $source=imagecreatefrompng($image); 
                    break;
            }
            imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

            switch($imageType) {
                case "image/gif":
                    imagegif($newImage,$image); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    imagejpeg($newImage,$image,90); 
                    break;
                case "image/png":
                case "image/x-png":
                    imagepng($newImage,$image);  
                    break;
            }

            chmod($image, 0777);
            return $image;
        }

        //You do not need to alter these functions    
        function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){

            list($imagewidth, $imageheight, $imageType) = getimagesize($image);
            $imageType = image_type_to_mime_type($imageType);

            $newImageWidth = ceil($width * $scale);
            $newImageHeight = ceil($height * $scale);
            $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

            switch($imageType) {
                case "image/gif":
                    $source=imagecreatefromgif($image); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    $source=imagecreatefromjpeg($image); 
                    break;
                case "image/png":
                case "image/x-png":
                    $source=imagecreatefrompng($image); 
                    break;
            }

            imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);

            switch($imageType) {
                case "image/gif":
                    imagegif($newImage,$thumb_image_name); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    imagejpeg($newImage,$thumb_image_name,90); 
                    break;
                case "image/png":
                case "image/x-png":
                    imagepng($newImage,$thumb_image_name);  
                    break;
            }

            chmod($thumb_image_name, 0777);

            return $thumb_image_name;

        }

        //You do not need to alter these functions
        function getHeight($image) {
            $size = getimagesize($image);
            $height = $size[1];
            return $height;
        }

        //You do not need to alter these functions
        function getWidth($image) {
            $size = getimagesize($image);
            $width = $size[0];
            return $width;
        }

    }   

?> 